package com.supermarket.manage.common.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-07 23:22
 **/
public class ListValidator implements ConstraintValidator<ListIsNotBlank, List> {

    private boolean require = false;

    @Override
    public void initialize(ListIsNotBlank constraintAnnotation) {
        require = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(List list, ConstraintValidatorContext constraintValidatorContext) {
        if (require) {
            return list != null && list.size() > 0;
        }
        return false;
    }
}
